package com.bit101.components
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	[Event(name = "open", type = "flash.events.Event")]

	/**
	 * 树组件单元
	 * @author feng 2016-3-21
	 */
	public class TreeItem extends ListItem
	{
		/**
		 * 箭头
		 */
		private var arrow:Sprite;

		/**
		 *
		 * @param parent
		 * @param xpos
		 * @param ypos
		 * @param data
		 */
		public function TreeItem(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, data:Object = null)
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			super(parent, xpos, ypos, data);
		}

		/**
		 * 深度
		 */
		public function get depth():int
		{
			return data.depth;
		}

		/**
		 * @private
		 */
		public function set depth(value:int):void
		{
			data.depth = value;
			invalidate();
		}

		/**
		 * 子节点数据
		 */
		public function get children():Array
		{
			try
			{
				return data.data.children;
			}
			catch (error:Error)
			{

			}
			return null;
		}

		override public function set data(value:Object):void
		{
			super.data = (value == "") ? null : value;
		}

		/**
		 * 打开
		 */
		public function get open():Boolean
		{
			return data.open;
		}

		public function set open(value:Boolean):void
		{
			data.open = value;
			openChildren();
			invalidate();
		}

		/**
		 * 打开子节点
		 */
		private function openChildren():void
		{
			dispatchEvent(new Event(Event.OPEN, true));
		}

		/**
		 * 添加事件
		 */
		protected function onAddedToStage(event:Event):void
		{
			arrow.addEventListener(MouseEvent.CLICK, onArrowClick);
		}

		/**
		 * 移除事件
		 */
		protected function onRemovedFromStage(event:Event):void
		{
			arrow.removeEventListener(MouseEvent.CLICK, onArrowClick);
		}

		/**
		 * @inheritDoc
		 */
		override protected function addChildren():void
		{
			initArrow();
			super.addChildren();
			_label.x = 20 * 4 / 5;
		}

		/**
		 * 初始化箭头
		 */
		private function initArrow():void
		{
			var size:int = 10;

			arrow = new Sprite();
			arrow.x = arrow.y = size;
			arrow.graphics.beginFill(0, 0);
			arrow.graphics.drawRect(-size, -size, size * 2, size * 2);
			arrow.graphics.endFill();
			arrow.graphics.beginFill(0x6f7777);
			arrow.graphics.moveTo(-size / 2, -size / 2);
			arrow.graphics.lineTo(size / 2, 0);
			arrow.graphics.lineTo(-size / 2, size / 2);
			arrow.graphics.lineTo(-size / 2, -size / 2);
			arrow.graphics.endFill();
			addChild(arrow);
		}

		/**
		 * @inheritDoc
		 */
		override public function draw():void
		{
			super.draw();
			if (data != null)
			{
				if (_data.data is String)
				{
					_label.text = _data.data as String;
				}
				else if (_data.data.hasOwnProperty("label") && _data.data.label is String)
				{
					_label.text = _data.data.label;
				}
				else
				{
					_label.text = _data.data.toString();
				}

				arrow.rotation = open ? 90 : 0;
				arrow.visible = children != null;
				this.x = depth * 30;
			}
		}

		/**
		 * 点击箭头事件
		 */
		protected function onArrowClick(event:MouseEvent):void
		{
			open = !open;
		}
	}
}
